<?php

namespace App\Admin\Controllers;

use App\Models\Property;
use Dcat\Admin\Form;
use Dcat\Admin\Grid;
use Dcat\Admin\Show;
use Dcat\Admin\Http\Controllers\AdminController;
use App\Models\Community;

class PropertyController extends AdminController
{
    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Grid::make(Property::with(['community']), function (Grid $grid) {
            $grid->column('id');
            $grid->column('community.community_name');
            $grid->column('address');
            //$grid->column('region_id');
            $grid->column('property_type')->using(Property::PROPERTY_TYPE);
            //$grid->column('building_age');
            $grid->column('floor');
            $grid->column('total_floors');
            $grid->column('room_count');
            $grid->column('living_area');
            $grid->column('total_area');
            $grid->column('orientation');
            $grid->column('decoration_status')->using(Property::DECORATION_STATUS);
            $grid->column('price');
            $grid->column('listing_price');
            $grid->column('lowest_price');
            $grid->column('property_rights')->using(Property::PEOPERTY_RIGHTS);
            $grid->column('mortgage_status')->using(Property::MORTGAGE_STATUS);
            //$grid->column('features');
            //$grid->column('images');
            //$grid->column('video_url');
            //$grid->column('vr_url');
            $grid->column('status')->using(Property::STATUS);
            $grid->column('listing_date');
            $grid->column('updated_at');

            $grid->selector(function (Grid\Tools\Selector $selector) {
                $selector->selectOne('status', '状态', Property::STATUS);
                $selector->selectOne('property_type', '房屋类型', Property::PROPERTY_TYPE);
                $selector->selectOne('decoration_status', '装修状态', Property::DECORATION_STATUS);
                $selector->selectOne('property_rights', '产权性质', Property::PEOPERTY_RIGHTS);
                $selector->selectOne('mortgage_status', '抵押状态', Property::MORTGAGE_STATUS);

            });

            $grid->filter(function (Grid\Filter $filter) {
                $filter->equal('community_id','楼盘名称')->select(Community::all()->pluck('community_name','id'));
            });
        });
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     *
     * @return Show
     */
    protected function detail($id)
    {
        return Show::make($id, new Property(), function (Show $show) {
            $show->field('id');
            $show->field('title');
            //$show->field('region_id');
            $show->field('address');
            $show->field('property_type');
            $show->field('building_age');
            $show->field('floor');
            $show->field('total_floors');
            $show->field('room_count');
            $show->field('living_area');
            $show->field('total_area');
            $show->field('orientation');
            $show->field('decoration_status');
            $show->field('price');
            $show->field('listing_price');
            $show->field('lowest_price');
            $show->field('property_rights');
            $show->field('mortgage_status');
            $show->field('features');
            $show->field('images');
            $show->field('video_url');
            $show->field('vr_url');
            $show->field('status');
            $show->field('listing_date');
            $show->field('created_at');
            $show->field('updated_at');
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Form::make(new Property(), function (Form $form) {
            $form->display('id');
            $form->text('title');
            //$form->text('region_id');
            $form->text('address');
            $form->year('building_age');
            $form->date('listing_date');
            $form->number('floor');
            $form->number('total_floors');
            $form->number('room_count');
            $form->currency('living_area')->width(4);
            $form->currency('total_area')->width(4);
            $form->text('orientation');
            $form->currency('price')->width(4);
            $form->currency('listing_price')->width(4);
            $form->currency('lowest_price')->width(4);
            $form->radio('status')->options(Property::STATUS)->default(0);
            $form->radio('property_type')->options(Property::PROPERTY_TYPE)->default(0);
            $form->radio('decoration_status')->options(Property::DECORATION_STATUS)->default(0);
            $form->radio('property_rights')->options(Property::PEOPERTY_RIGHTS)->default(0);
            $form->radio('mortgage_status')->options(Property::MORTGAGE_STATUS)->default(0);
            $form->textarea('features');
            $form->multipleImage('images');
            $form->file('video_url');
            $form->file('vr_url');

        });
    }
}
